package com.teamclub.base.confs;

import com.google.common.collect.Lists;
import com.teamclub.util.routes.RouterConfigurationSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.util.List;

/**
 * Created by ilkkzm on 17-4-19.
 */
//@Configuration
// disable it.
public class ApplicationConfig extends RouterConfigurationSupport {
    private Logger logger = LoggerFactory.getLogger(ApplicationConfig.class);
    @Override
    public List<Resource> listRouteFiles () {
        try {
            ClassLoader cl = ApplicationConfig.class.getClassLoader();
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
            Resource[] resources = resolver.getResources("classpath*:*.routes");
            logger.info("find {} resources", resources.length);
            List<Resource> routeFiles = Lists.newArrayList();
            for(Resource resource: resources) {
                try {
                    logger.info("add {} to route files", resource.getURI());
                    routeFiles.add(resource);
                } catch(Exception e) {
                    logger.warn("ignore {}", resource);
                }

            }
            logger.info("valid route length: {}", routeFiles.size());
            return routeFiles;
        }catch (Exception e) {
            logger.error("", e);
            return Lists.newArrayList();
        }

    }
}
